'''
Author: your name
Date: 2021-03-02 17:42:55
LastEditTime: 2021-03-02 17:59:03
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \flutter\python\main.py
'''
# -*- coding: utf-8 -*-
import sqlite3
from bottle import route, run, static_file, template, request, error, response
from json import dumps

# 静态资源配置
@route('/static/<filetype>/<filename>') 
def server_static(filetype, filename):
    return static_file(filename, root='./static/'+filetype)

@route('/todo')
def todo_list():
    return template("./index.html")

@route('/getlist')
def get_arrdata():
   
    response.content_type = 'application/json'
    res = []
    with open("../check_test.log") as file_obj:
        for line in file_obj:
            if "NVMETEMP" not in line:
                arr_line = []
                line = line.strip("\n").split(",")
                for item in line:
                    if item.isalnum():
                        arr_line.append(int(item))
                    else:
                        arr_line.append(item)
                res.append(arr_line)
    return dumps(res)  

@route('/getIoSpeed/<threadId>')
def get_ioSpeed(threadId):
    response.content_type = 'application/json'
    res = []
    preNum = 0
    preTim = 0
    with open("/home/han/linux_cpu/log/log_statis_width_"+threadId+".log") as file_obj:
        for line in file_obj:
            arr_line = []
            line = line.strip("\n").split(",")
            wid = int(line[1].split(".")[0]);
            arr2 = line[0].split(":")
            tim = int(arr2[2]) + int(arr2[1])*60 + int(arr2[0])*60*60
            if(0 == preNum):
                preNum = wid
                preTim = tim
                continue
            diff = (wid - preNum)/ (tim - preTim)
            # print(wid, preNum, tim, preTim)
            # print(wid-preNum, tim-preTim)
            # print()
            arr_line.append(line[0])
            arr_line.append(round(diff, 2))
            res.append(arr_line)
            preNum = wid
            preTim = tim
    return dumps(res)

@error(404)
def mistake404(code):
    return 'Sorry, page is losing !!! '
    
run(reload=True, host='0.0.0.0', port=8090)
